<template>
  <div class="dynamic-table-box">
    <h1>表格校验</h1>
    <el-form
      ref="tableForm"
      :model="tableForm"
      style="width: 450px;"
    >
      <el-table
        :data="tableForm.tableData"
        border
        style="width: 100%;"
      >
        <!--el-form-item 里面的循环prop名字，需要 和form中list的名字一致 -->
        <el-table-column label="姓名">
          <template #default="scope">
            <el-form-item
              :prop="`tableData.${scope.$index}.name`"
              :rules="tableRules.name"
            >
              <el-input
                v-model="scope.row.name"
                style="width: 180px;"
              />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column label="年龄">
          <template #default="scope">
            <el-form-item
              :prop="`tableData.${scope.$index}.age`"
              :rules="tableRules.age"
            >
              <el-input
                v-model="scope.row.age"
                style="width: 180px;"
              />
            </el-form-item> 
          </template>
        </el-table-column>
      </el-table>
      <el-form-item mt-20>
        <el-button
          type="primary"
          @click="submitForm"
        >
          提交
        </el-button>
        <el-button @click="resetForm">
          重置
        </el-button>
      </el-form-item>
    </el-form>
    <el-form
      ref="form"
      :model="form"
      label-position="top"
      style="width: 450px;"
    >
      <el-form-item label="限制条件性的输入">
        <el-input
          v-model="form.value1"
          placeholder="只能输入数字"
          style="width: 180px;"
          @input="handleInput"
        />
        <el-input
          v-model="form.value2"
          mt-20
          ml-20
          placeholder="只能输入数字和小数"
          style="width: 180px;"
          @input="value => value.replace(/[^\d.]/g,'')"
        />
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { defineComponent,reactive,ref,onBeforeMount,getCurrentInstance } from "vue"
export default defineComponent({
  name:"DynamicTable",
  setup() {
    let vm = ""
    const tableForm = reactive({
      tableData:[
        { name:"",age:"" }
      ]
    })

    const tableRules = ref({
      name:[
        {
          required:true,
          message:"请输入姓名",
          trigger:["blur","change"]
        },{
          max:20,
          message:"长度在20个字符以内",
          trigger:["blur","change"]
        }  
      ],
      age:[
        {
          required:true,
          message:"请输入年龄",
          trigger:["blur","change"]
        },{
          max:20,
          message:"长度在20个字符以内",
          trigger:["blur","change"]
        }
      ]
    })

    const form = reactive({
      value1:"",
      value2:""
    })

    const submitForm = () => {
      vm.refs.tableForm.validate(valid => {
        if(valid) {
          console.log("dymic-table提交成功")
        }else {
          console.log("dymic-table提交失败")
          return false
        }
      })
    }

    const resetForm = () => {
      vm.refs.tableForm.resetFields()
    }


    const handleInput = value => {
      console.log(value.replace("/[^\d]/g",""))
      // return value = value.replace("/[^\d]/g","")
    }

    onBeforeMount(() => {
      vm = getCurrentInstance()
    })

    return {
      tableForm,
      form,
      tableRules,
      submitForm,
      resetForm,
      handleInput
    }
  }
})
</script>
<style lang='less' scoped>
  .dynamic-table-box {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
  }
</style>